class Solution
{
public:
    int maxSatisfied(vector<int> &customers, vector<int> &grumpy, int minutes)
    {
        int kSum = 0;
        for (int i = 0; i < minutes; ++i)
        {
            kSum += customers[i] * grumpy[i];
        }

        int n = grumpy.size();
        int maxKSum = kSum;
        int maxKSumIndex = 0;
        for (int i = minutes; i < n; ++i)
        {
            kSum -= customers[i - minutes] * grumpy[i - minutes];
            kSum += customers[i] * grumpy[i];
            if (kSum > maxKSum)
            {
                maxKSum = kSum;
                maxKSumIndex = i - minutes + 1;
            }
        }

        int result = 0;
        for (int i = 0; i < n; ++i)
        {
            if (i >= maxKSumIndex && i < maxKSumIndex + minutes)
            {
                result += customers[i];
            }
            else if (!grumpy[i])
            {
                result += customers[i];
            }
        }
        return result;
    }
};